import { PrismaClient } from "@prisma/client";
import express from "express";
const prisma = new PrismaClient();
const router = express.Router();

router.get("/get", async function (req, res) {
  const apps = await prisma.app.findMany({});
  return res.json({
    code: 0,
    data: apps,
  });
});
router.post("/add", async function (req, res) {
  const { appName, rate, phoneNumber } = req.body;
  if (!appName) {
    return res.json({
      code: -1,
      msg: "appName必填",
    });
  }
  if (!phoneNumber) {
    return res.json({
      code: -1,
      msg: "phoneNumber必填",
    });
  }
  try {
    await prisma.app.create({
      data: {
        appName,
        rate: Number(rate),
        phoneNumber,
      },
    });
    return res.json({
      code: 0,
      data: [],
    });
  } catch (e) {
    return res.json({
      code: -1,
      msg: e,
    });
  }
});

export { router };
